package com.scalar.chiptrack.adaptor.spil;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.MutableAttributeSet;
import java.io.Reader;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: May 4, 2007
 * Time: 7:36:33 PM
 */
public class SPILHtmlParser extends HTMLEditorKit.ParserCallback
{

    String downLoadFileName;

    public void parse( Reader reader) throws Exception
    {
        DTD dtd = DTD.getDTD( "HTML" );
        DocumentParser documentParser = new DocumentParser( dtd );
        documentParser.parse( reader, this, true /*ignore CharSet*/ );

    }
    /**
     *
     * @param t
     * @param a
     * @param pos
     */
    public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
    {
        if (t.toString().equals("iframe"))
        {
            HTML.Attribute name = HTML.getAttributeKey("src");
            HTML.Attribute value = HTML.getAttributeKey("value");

            if (a.getAttribute(name) != null && a.getAttribute(name).toString().startsWith("DIR/tmp/"))
            {
                String fileName = (String) a.getAttribute(name);
                fileName = fileName.substring(8, 26);
                setDownLoadFileName(fileName);
            }
        }
    }
    /**
     *
     * @param data
     * @param pos
     */
    public void handleText(char[] data, int pos)
    {
        String text = new String(data);
    }

    public String getDownLoadFileName()
    {
        return downLoadFileName;
    }

    public void setDownLoadFileName(String downLoadFileName)
    {
        this.downLoadFileName = downLoadFileName;
    }
}
